/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.openbookformat.project;

import java.awt.Image;
import javax.swing.Action;
import org.netbeans.spi.project.ui.LogicalViewProvider;
import org.netbeans.spi.project.ui.support.CommonProjectActions;
import org.openide.filesystems.FileObject;
import org.openide.loaders.DataFolder;
import org.openide.nodes.FilterNode;
import org.openide.nodes.Node;
import org.openide.util.ImageUtilities;
import org.openide.util.Lookup;
import org.openide.util.lookup.Lookups;
import org.openide.util.lookup.ProxyLookup;

/**
 *
 * @author MUFEED
 */
public class OBFProjectLogicalView implements LogicalViewProvider{
    private OBFProject project;
    private ProjectRootNode projectNode;

    public OBFProjectLogicalView(OBFProject project) {
        this.project = project;
    }

    @Override
    public Node createLogicalView() {
        DataFolder projectDir = DataFolder.findFolder(project.getProjectDirectory());
        Node rootNode = projectDir.getNodeDelegate();
        projectNode =  new ProjectRootNode(rootNode, project);
        return projectNode;
    }

    public ProjectRootNode getProjectNode() {
        return projectNode;
    }
    
    @Override
    public Node findPath(Node node, Object o) {
        //throw new UnsupportedOperationException("Not supported yet.");
        return node;
    }

    static class ProjectRootNode extends FilterNode {
        final OBFProject project;

        public ProjectRootNode(Node node, OBFProject project) {
            super(node, new FilterNode.Children(node),
                    new ProxyLookup(new Lookup[]{
                        Lookups.singleton(project),
                        node.getLookup()
            }));
            this.project = project;
        }

        @Override
        public Action[] getActions(boolean arg0) {
            Action[] nodeActions = new Action[7];
            nodeActions[0] = CommonProjectActions.newFileAction();
            nodeActions[1] = CommonProjectActions.copyProjectAction();
            nodeActions[2] = CommonProjectActions.deleteProjectAction();

            nodeActions[5] = CommonProjectActions.setAsMainProjectAction();
            nodeActions[6] = CommonProjectActions.closeProjectAction();
            return nodeActions;
        }

        @Override
        public Image getIcon(int type) {
            return ImageUtilities.loadImage("org/openbookformat/project/library.png");
        }

        @Override
        public String getDisplayName() {
            return project.getProjectDirectory().getName();
        }

        @Override
        public Image getOpenedIcon(int type) {
            return getIcon(type);
        }
    }

    private static class BookNode extends FilterNode {
        final FileObject book;

        public BookNode(Node node, FileObject book) {
            super(node, new FilterNode.Children(node),
                    new ProxyLookup(new Lookup[]{
                        Lookups.singleton(book),
                        node.getLookup()
            }));
            this.book = book;
        }

        @Override
        public Image getIcon(int type) {
            return ImageUtilities.loadImage("org/openbookformat/project/book_open.png");
        }

        @Override
        public String getDisplayName() {
            return book.getName();
        }

        @Override
        public Image getOpenedIcon(int type) {
            return getIcon(type);
        }

    }

}
